package com.doudou.collection.service.impl;

import com.doudou.collection.mapper.AnnouncementMapper;
import com.doudou.collection.pojo.dto.AnnouncementAddNewDTO;
import com.doudou.collection.pojo.entity.Announcement;
import com.doudou.collection.pojo.vo.AnnouncementEditVO;
import com.doudou.collection.pojo.vo.AnnouncementVO;
import com.doudou.collection.response.ResultVO;
import com.doudou.collection.security.CustomUserDetails;
import com.doudou.collection.service.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AnnouncementServiceImpl implements AnnouncementService {


    @Autowired
    private AnnouncementMapper announcementMapper;

    /**
     * 发布公告业务功能
     * @param announcementAddNewDTO
     * @return
     */
    @Override
    public ResultVO addNew(AnnouncementAddNewDTO announcementAddNewDTO, CustomUserDetails userDetails) {

        log.debug("开始处理【发布公告】业务: {}", announcementAddNewDTO);

        Announcement announcement = new Announcement();
        BeanUtils.copyProperties(announcementAddNewDTO, announcement);


        if (announcementAddNewDTO.getId() == null) {

            announcement.setCreateTime(new Date());
            announcement.setModifiedTime(new Date());
            announcementMapper.insertAnnouncement(announcement);

        } else {
            log.debug("开始处理【修改公告】业务: {}", announcementAddNewDTO);

            announcement.setModifiedTime(new Date());
            announcement.setUserId(userDetails.getId());
            announcementMapper.updateAnnouncement(announcement);

            log.info("修改成功：{}", announcement);
        }

        return ResultVO.ok();

    }

    /**
     * 查询所有公告业务功能
     * @return
     */
    @Override
    public ResultVO selectAll() {
        log.debug("开始处理【查询所有公告】业务");
        List<AnnouncementVO> list = announcementMapper.selectAll();

        log.info("查询成功: {}", list);
        return ResultVO.ok(list);
    }

    /**
     * 编辑公告业务功能
     * @param id
     * @return
     */
    @Override
    public ResultVO editAnnouncementById(Long id) {
        log.debug("开始处理【编辑公告】业务: {}", id);

        AnnouncementEditVO announcementEditVO = announcementMapper.selectByIdForEdit(id);

        return ResultVO.ok(announcementEditVO);
    }

    /**
     * 修改撤回状态业务功能
     * @param id
     * @param ifCancel
     * @return
     */
    @Override
    public ResultVO updateIfCancel(Long id, Integer ifCancel) {
        log.debug("开始处理【修改撤回状态】业务：公告ID:{}, 撤回状态:{}", id, ifCancel);

        Announcement announcement = new Announcement();
        announcement.setId(id);
        announcement.setIfCancel(ifCancel);
        announcement.setCancelTime(new Date());

        announcementMapper.updateIfCancel(announcement);
        return ResultVO.ok();
    }

    /**
     * 修改公告撤回状态功能
     * @param id
     * @param delFlag
     * @return
     */
    @Override
    public ResultVO updateDelFlag(Long id, Integer delFlag) {
        log.debug("开始处理【修改删除状态】业务：公告ID:{}, 撤回状态:{}", id, delFlag);

        Announcement announcement = new Announcement();
        announcement.setId(id);
        announcement.setDelFlag(delFlag);
        announcement.setDeleteTime(new Date());

        announcementMapper.updateDelFlag(announcement);
        return ResultVO.ok();
    }
}
